<?php
	$form = $_POST;
	$news = new News();
	$news->setname($form['news_name']);
	$news->setdescription($form['description']);
	$news->setcontent($form['content']);
	$news->setactive(isset($form['active']) ? 1 : 0);
	if (!$news_id)
		$news->setcreated_at(date("Y/m/d"));
	$news->setupdated_at(date("Y/m/d"));
	
	$prefixes = array();
	$prefixes[] = 'tour_price_';
	
	$pic_id = check_and_upload_picture('thumbnail', $form['thumbnail'], $HTTP_POST_FILES, $prefixes);
	if ($pic_id) {
		// TODO: remove the old one
		if ($news_id) {
			$news->select($news_id);
			$old_pic_id = $news->getthm_picture();
			$news->setthm_picture($pic_id);
			$news->update();
			if ($old_pic_id) {
				$old_pic = new Picture();
				$old_pic->select($old_pic_id);
				$old_pic->deleteAllFiles();
				$old_pic->delete();
			}
		}
		$news->setthm_picture($pic_id);
		
	} elseif ($news_id) {
		$tmp_news = new News();
		$tmp_news->select($news_id);
		$news->setthm_picture($tmp_news->getthm_picture());
	}
		
	if ($news_id) { // it's an update action
		$news->update($news_id);
	} else { // it's an create action
		$news->insert();
	}

	$msg = $news_id ? "Tin tức đã được sửa thành công" : "Tin tức đã được thêm thành công";
	admin_load($msg, "?act=news_manager");
?>